package com.gewu.bookeeping.View.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;

import com.gewu.bookeeping.MyDb.bean.ALIBeanIn;
import com.gewu.bookeeping.MyDb.bean.ALiBeanOut;
import com.gewu.bookeeping.MyDb.bean.WeChtBeanIn;
import com.gewu.bookeeping.MyDb.bean.WeChtBeanOut;
import com.gewu.bookeeping.R;
import com.gewu.bookeeping.View.ItemFragment.ForOneItem1Fargemnt;
import com.gewu.bookeeping.View.ItemFragment.ForOneItem2Fargemnt;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ItemActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private ViewPager viewpager;
    private RadioButton one;
    private RadioButton two;
    private WeChtBeanOut weChtBeanOut;
    private WeChtBeanIn weChtBeanIn;
    private ALiBeanOut aLiBeanOut;
    private ALIBeanIn aliBeanIn;
    private int values;
    private int type;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item);
        initView();
        initViewpager();
    }

    private void initViewpager() {
        fragments.add(new ForOneItem1Fargemnt());
        fragments.add(new ForOneItem2Fargemnt());

        viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });

        viewpager.setCurrentItem(0, true);
        viewpager.setOffscreenPageLimit(fragments.size());

        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    one.setChecked(true);
                    two.setChecked(false);
                } else {
                    one.setChecked(false);
                    two.setChecked(true);
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
       switch (type){
           case 0:
               // 0 weixin   1 Ali
               if (values == 0) {
                   //0  收入  1 支出
                   new ForOneItem1Fargemnt().add(0, 0);
               } else {
                   new ForOneItem1Fargemnt().add(1, 0);
               }
               break;
           case 1:
               if (values == 0) {
                   new ForOneItem1Fargemnt().add(0, 1);
               } else {
                   new ForOneItem1Fargemnt().add(1, 1);
               }
               break;
           case 2:
               if (values == 0) {
                   new ForOneItem1Fargemnt().add(0, 2);
               } else {
                   new ForOneItem1Fargemnt().add(1, 2);
               }
               break;
       }

    }

    private void initView() {
        toolbar = findViewById(R.id.toolbar);
        viewpager = findViewById(R.id.viewpager);
        toolbar.setTitle("单个消费分析");
        one = findViewById(R.id.one);
        two = findViewById(R.id.two);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        Bundle date = getIntent().getBundleExtra("date");
        type = date.getInt("type");
        values = date.getInt("values");
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}